import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import { getCartList } from "../api/index.js"

export default new Vuex.Store({
  state: {
    cartList: [], // 购物车数据
  },
  getters: {
    cartNum(state) {
      return state.cartList.reduce((prev, next) => {
        return prev + next.goodsCount
      }, 0)
    }
  },
  mutations: {
    changeCartList(state, list) {
      state.cartList = list.map(item => {
        item.checked = true;
        return item;
      })
    }
  },
  actions: {
    changeCartListAsync({ commit }) {
      getCartList().then(data => {
        // console.log("data:", data);
        commit("changeCartList", data.data || [])
      })
    }
  },
  modules: {
  }
})
